<?php

function parse_level($values,$name,$start,$level){
//		echo "parsing object level: $level, name: $name, start: $start\n";
		$obj="";
		$i=$start;
		while($values[$i]['tag']!=$name and $values[$i]['type']!="close"){
			if($values[$i]['type']=="complete"){
				$obj[$values[$i]['tag']]=$values[$i]['value'];
				$i++;				
			}
			if($values[$i]['type']=="open"){
				$i++;
				$obj[$values[$i-1]['tag']]=parse_level($values,$values[$i-1]['tag'],&$i,$level+1);
			}
		}
		$start=$i+1;
		return $obj;
}


function GetItem($ori_key,$ori_values,$ind){
		$item_list=$ori_key['Item'];
		$item_start=$item_list[2*$ind];
		$obj=parse_level($ori_values,"Item",$item_start+1,0);
		return $obj;
}


?>
<html>

<head>
<LINK REL="stylesheet" HREF="http://www.jp-life.org/shipit/my.css">
</head>
<body>
<br>

<h1>Aha! Let's do search!</h1>

<form action="parsexml.php" method="get">
Key word: <input type="text" name="key" value="" size=20>

<select name="index">
<option>all</option>";
}<?php
$array = array(Blended,Books,Classical,DVD,Electronics,ForeignBooks,Kitchen,Music,MusicalInstruments,MusicTracks,Software,Toys,VHS,Video,VideoGames);
foreach($array as $value) 
{
	print "<option>$value </option>\n";
}
?> 
</select>
	<input type="submit" value="Do request">    

</form>


<?php

$dev_token="0NVB1DNG62VWBEECWCR2";


function CreateHtml($Item){

	$str="<table border=0 width='90%'><tr>\n";
	$str.="<td width='200' align='left'>";
	if($Item['SmallImage']['URL']){
	$str.= "<img align='left' src=\"". $Item['SmallImage']['URL']."\"><br>\n";
	}
	else{
	$str.= "No Image";
	}
	$str.="</td>\n<td>";
	$str.= $Item['ItemAttributes']['Title']."<br>\n";
	$str.= "Price: ".$Item['ItemAttributes']['ListPrice']['FormattedPrice']."<br>";
	$str.= "<a href='".$Item['DetailPageURL']."'> Order it!</a>\n";
	$str.="</td>\n<td width='150'>Add this to catalog <input type=\"checkbox\" value=\"".$Item['ASIN']."\" >";
	$str.="</td></tr></table>\n<hr size=1>\n";
	
	return $str;
}



$aff_tag="";
$xml="";

//Amaon variables.
$pd_name="";
$image_url="";
$price="";
$amazon_url="";

$key=$_GET['key'];
$S_index=$_GET['index'];


if($key and $S_index)
{

	if($S_index=="all"){
	$URL="http://webservices.amazon.com/onca/xml?Service=AWSECommerceService&SubscriptionId=$dev_token&Operation=ItemSearch&Keywords=$key&ResponseGroup=Medium";
	}
	else{
	$URL="http://webservices.amazon.com/onca/xml?Service=AWSECommerceService&SubscriptionId=$dev_token&Operation=ItemSearch&SearchIndex=$S_index&Keywords=$key&ResponseGroup=Medium";
	}
	
	$aurl=fopen($URL,"r");
	while(!feof($aurl))
	{
		$xml.=fgets($aurl,4096);
	}
	fclose($aurl);



	$parser = xml_parser_create();
	xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING,0);

	xml_parse_into_struct($parser,$xml,$values,$index);

	xml_parser_free($parser);


	print "<BR>sample Value: ";

	echo "\n";
	for($i=0;$i<10;$i++){
		$Item=GetItem($index,$values,$i);
		echo CreateHtml($Item);
	}

}

	print "<br><h2>$URL</h2>";

?>



<br>
<hr>
Created by Liu Xuefeng.<br>
Started on 2006/12/27

</body>
</html>
